package es.ihatetothink.restaurantesCeliacos;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;

import android.util.Log;



public class XmlParser {
	private URL Url;
	private newrestaurante restauranteActual;
	public XmlParser(String url){
		

		try {
			this.Url=new URL(url);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private ArrayList<String> getLinks()
	{
		HtmlCleaner parser = new HtmlCleaner();
		CleanerProperties props = parser.getProperties();
		props.setAllowHtmlInsideAttributes(true);
		props.setAllowMultiWordAttributes(true);
		props.setRecognizeUnicodeChars(true);
		props.setOmitComments(true);
		ArrayList<String> ListadoLinks = new ArrayList<String>();
		try {
			TagNode node = parser.clean(Url.openStream());
			TagNode[] listado =node.getElementsByAttValue("id", "listado", true, false);
			TagNode[] links = listado[0].getElementsByName("a", true);
			
			Log.i("celiacos","TOTAL LINKS: "+links.length);
			for (TagNode i : links){
				String temp= i.getAttributeByName("href").toString();
				if(!ListadoLinks.contains(temp)){
					ListadoLinks.add(temp);
					Log.i("celiacos",temp);
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return ListadoLinks;
		
	}
	public void parse(){
		for (String casiLink : getLinks()){
			try {
				URL Link = new URL("http://www.celiacscatalunya.org/"+casiLink);
				
				
				
				
				
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	


}